package com.aptech.fpt.c0810g.newspj.dataaccess;

import com.aptech.fpt.c0810g.newspj.business.DanTriDataProcess;
import com.aptech.fpt.c0810g.newspj.dataaccess.share.GetConnection;
import com.aptech.fpt.c0810g.newspj.dataaccess.share.InvokeSelectQuery;
import com.aptech.fpt.c0810g.newspj.entity.News;
import com.aptech.fpt.c0810g.newspj.entity.Source;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.w3c.dom.NodeList;

/**
 *
 * @author zang
 */
public class NewsAction {

    /***
     * @author AnhGT
     * @param pNewsID Ma tin tuc
     * @return Danh sach tin tuc theo ID
     */
    public ArrayList<News> getNewsListByID(int pNewsID) {
        //Bang muon truy van
        String tableName = "News";
        ArrayList<News> list = new ArrayList<News>();
        //Cac cot muon truy van
        ArrayList<String> colums = new ArrayList<String>();
        colums.add("ID");
        colums.add("Title");
        colums.add("SubContent");
        colums.add("Photo");
        colums.add("SourceID");
        colums.add("SourceLink");
        colums.add("MainContent");
        colums.add("Catelogy");

        //Cac dieu dien 
        ArrayList<String> codition = new ArrayList<String>();
        codition.add("ID=" + pNewsID);

        ArrayList<HashMap<String, Object>> records = new InvokeSelectQuery().getList(colums, tableName, codition);
        //add gia tri
        for (int i = 0; i < records.size(); i++) {
            News newList = new News();
            newList.setId(Integer.valueOf(records.get(i).get("ID").toString()));
            newList.setTitle(records.get(i).get("Title").toString());
            newList.setSubContent(records.get(i).get("SubContent").toString());
            newList.setPhoto(records.get(i).get("Photo").toString());
            newList.setSourceID(pNewsID);
            newList.setMainContent(records.get(i).get("MainContent").toString());

            ArrayList<Source> sourceList = getNewsSource(Integer.valueOf(records.get(i).get("SourceID").toString()));
            newList.setSourceObj(sourceList.get(0));
            list.add(newList);
        }
        return list;
    }

    /***
     * @author AnhGT
     * @param pSourceID Ma nguoi muon lay
     * @return Lay ra thong tin nguon trang
     */
    public ArrayList<Source> getNewsSource(int pSourceID) {
        ArrayList<Source> list = new ArrayList<Source>();
        //Cac cot muon truy van
        ArrayList<String> colums = new ArrayList<String>();
        colums.add("Name");
        colums.add("HomePage");
        colums.add("Logo");
        //Cac dieu dien 
        ArrayList<String> codition = new ArrayList<String>();
        codition.add("ID=" + pSourceID);
        //Bang muon truy van
        String tableName = "Source";
        ArrayList<HashMap<String, Object>> records = new InvokeSelectQuery().getList(colums, tableName, codition);
        //add gia tri
        for (int i = 0; i < records.size(); i++) {
            Source sourceOBj = new Source();
            sourceOBj.setId(pSourceID);
            sourceOBj.setName("Name");
            sourceOBj.setHomePage("HomePage");
            sourceOBj.setPhoto("Logo");
            list.add(sourceOBj);
        }
        return list;
    }

    /**
     * @param pSourceLink link can kiem tra
     * @return Ket qua link nay da ton tai hay chua
     *
     */
    public boolean checkSourceURLExist(String pSourceLink) {
        ArrayList<Source> list = new ArrayList<Source>();
        //Cac cot muon truy van
        ArrayList<String> colums = new ArrayList<String>();
        colums.add("SourceLink");
        //Cac dieu dien
        ArrayList<String> codition = new ArrayList<String>();
        codition.add("SourceLink='" + pSourceLink + "'");
        //Bang muon truy van
        String tableName = "News";
        ArrayList<HashMap<String, Object>> records = new InvokeSelectQuery().getList(colums, tableName, codition);
        //add gia tri
        if (records.size() > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @return Danh sach bang tin
     */
    public ArrayList<News> doGetNews() {
        ArrayList<News> list = new ArrayList<News>();
        DanTriDataProcess process = new DanTriDataProcess();
        NodeList nodes = process.getNodeList("http://dantri.com.vn/c36s36/thegioi/trang-1.htm", "mt3 clearfix");
        list = process.getTextContent(nodes);
        //kiem tra tin da duoc lay ve hay chua
        for (News news : list) {
            // = true la da ton tai, nguoc lai la flase
            boolean check = this.checkSourceURLExist(news.getSourceLink());
            news.setExist(check);
        }
        return list;
    }

    /**
     * @param listNews Danh sach cac tin duoc chon de lay ve
     * @return Ket qua co lay ve thanh cog hay khong
     */
    public boolean insertNewsContent(ArrayList<News> listNews) throws SQLException {
        boolean flag = false;
        PreparedStatement prepareStatement = null;
        Connection conn = null;
        try {
            String INSERT_NEWS = "INSERT INTO News VALUES(?,?,?,?,?,?,?)";
            conn = new GetConnection().GetObj();
            conn.setAutoCommit(false);
            prepareStatement = conn.prepareStatement(INSERT_NEWS);

            for (News news : listNews) {
                prepareStatement.setInt(1, news.getCatelogy());
                prepareStatement.setString(2, news.getTitle());
                prepareStatement.setString(3, news.getSubContent());
                prepareStatement.setString(4, news.getPhoto());
                prepareStatement.setString(5, news.getMainContent());
                prepareStatement.setInt(6, news.getSourceID());
                prepareStatement.setString(7, news.getSourceLink());
                prepareStatement.addBatch();
            }
            //thuc thi batch
            int[] result = prepareStatement.executeBatch();
            if (result.length == listNews.size()) {
                flag = true;
            }

            conn.commit();

        } catch (SQLException ex) {
            Logger.getLogger(NewsAction.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            prepareStatement.close();
            conn.close();
        }
        return flag;
    }

    /**
     * @param Ma tin tuc muon xoa
     * @return Co xoa thanh cong hay khong
     */
    public boolean deleteNews(int pID) throws SQLException {
        Connection conn = null;
        PreparedStatement prmt = null;
        boolean result = false;
        try {
            conn = new GetConnection().GetObj();
            String sql = "DELETE FROM News WHERE ID = " + pID;
            prmt = conn.prepareStatement(sql);

            int index = prmt.executeUpdate();
            if (index == 1) {
                result = true;
            }
        } catch (Exception e) {
        }finally {
            conn.close();
            prmt.close();
        }
        return result;
    }
}
